//
//  DetailsView.m
//  LPlates.app
//
//  Created by Andreas Schwelling on 23.11.07.
//	http://www.andi.de/licenseplates/
//
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.



#import "DetailsView.h"

@implementation DetailsView


-(void) loadData:(id)p
{
	
	td = [MobileVehicleRegPlates sharedInstance];
	
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: @"/Applications/LPlates.app/default.plist"];
	
	NSArray *keys,*keys1;
	int k,j, count,count1;
	id key, value,  value1;
	
	keys = [dict allKeys];
	count = [keys count];
	
	int countycount = 0;
	
	for (j = 0; j < count; j++)
	  {
		  key = [keys objectAtIndex: j];
		  value = [dict objectForKey: key];
		  
		  keys1 = [value allKeys];
		  count1 = [keys1 count];
		  
		  
		  NSDictionary *inner = [dict objectForKey: @"Countries"];
		  NSDictionary *inner1 = [inner objectForKey: [td getCountry]];
		  //NSLog (@"Key: %@ ", [inner1 allKeys] );
		  
		  
		  NSEnumerator *keyenum = [inner1 keyEnumerator];
		  
		  while ((key = [keyenum nextObject])) {
			  //NSLog(@"%@",key);
			  
			  // Each "header cell" here is a dictionary with two keys:
			  // title points to a NSSTring of what we want the header to say
			  // beginRow is a NSNumber integer that dictates which row this header will come before
			  
			  
			  
			  NSDictionary *inner2 = [inner1 objectForKey:key];
			  NSEnumerator *keyenum1 = [inner2 keyEnumerator];
			  id key1;
			  while ((key1 = [keyenum1 nextObject])) {
				  
				  
				  if ([key1 isEqualToString:[td getCounty]]) {
					  NSDictionary *inner3 = [inner2 objectForKey:key1];
					  //NSLog(@"jaja");
					  [td setDescription:[inner3 objectForKey:@"descr"]];
					  
					  id stateid = [inner3 objectForKey:@"state"];
					  
					  [td setState:stateid];
					  
					  //			state = [inner3 objectForKey:@"state"];
				  }
				  
				  countycount++;
				  
			  }
			  
			  
		  }	
		  
		  
		  
	  }
	
	[table reloadData];
	NSLog(@"descr: %@",[destr description]);
	
	
}

-(id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	
	//[self loadData];
	
	
	td = [MobileVehicleRegPlates sharedInstance];
	
	CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	
	
	
	table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0f, 45.0f, 320.0f, 415.0f)];
	[table setDataSource:self];
	[table setDelegate:self];
	
	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    float transparentComponents[4] = {0, 0, 0, 0};
    float grayComponents[4] = {0.85, 0.85, 0.85, 1};
    
    float foreground[4] = {0.25, 0.22, 0.25, 1};
	
    CGColorSpaceRef colorShadow = CGColorSpaceCreateDeviceRGB();
	CGSize size;
    size.height = 1;
    size.width = 1;
    
    citycellTitle = @"District/City";
    
	UITextLabel *text;
    text  = [[UITextLabel alloc] initWithFrame:
			 CGRectMake(15.0f, 15.0f, 160.0f, 20.0f)];
    [ text setShadowColor: CGColorCreate(colorShadow, grayComponents)];
    [ text setShadowOffset: size ];
    [ text setText:@"License plate code"];
    [ text setColor: CGColorCreate(colorSpace, foreground)];
	struct __GSFont* font = [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:0 size:16];
    [ text setBackgroundColor:
	 CGColorCreate(colorSpace, transparentComponents)];
    [ table addSubview: text ];
    [ text release ];
	
    text  = [[UITextLabel alloc] initWithFrame:
			 CGRectMake(15.0f, 100.0f, 160.0f, 20.0f)];
    [ text setShadowColor: CGColorCreate(colorShadow, grayComponents)];
    [ text setShadowOffset: size ];
    [ text setText:@"Location"];
    [ text setColor: CGColorCreate(colorSpace, foreground)];
	
    [ text setFont: font];
	// CFRelease(font);
    [ text setBackgroundColor:
	 CGColorCreate(colorSpace, transparentComponents)];
    [ table addSubview: text ];
    [ text release ];
	
	[table reloadData];
	
	navBar = [[UINavigationBar alloc] init];
	[navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
	[navBar showButtonsWithLeftTitle:@"Back" rightTitle:@"About" leftBack:YES];
	[navBar setDelegate:self];
	[navBar enableAnimation];
	[navBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle: @"Details"]];
	
	[self addSubview:navBar];
	[self addSubview:table];
	
	[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(loadData:) userInfo:nil repeats:NO];
	
	//NSLog(@"d: %@",[td getDescription]);
	return self;
}

-(void)dealloc{
	
	[navBar release];
	[table release];
	[super dealloc];
	
}

- (void)reloadData
{
	[table reloadData];
}

- (void)setDelegate: (id)delegate
{
    _delegate = delegate;
}

#pragma mark ----------------Delegate Methods----------------



- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	if (button == 0) [_delegate showAboutSheet];
	else
		[_delegate hideDetailView];
	
}
- (void)tableRowSelected:(NSNotification *)notification
{
	citycellAlign = 0;
	citycellTitle = @"District/City";
	if ([table selectedRow] == 11)
	  {
		  id country = [td getCountry];
		  
		  NSString* name = [td getDescription];
		  
		  NSString *urlstring = [NSString stringWithFormat:@"http://maps.google.com/?q=%@ %@",name, country];
		  NSString *fixedURL = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)urlstring, NULL, NULL, kCFStringEncodingUTF8);
		  
		  [td openURL:[NSURL URLWithString:fixedURL]];
		  
	  } else
		  if ([table selectedRow] == 7) {
			  NSLog(@"sel");
			  citycellAlign = 2;
			  citycellTitle = @"";
			  
			  
		  }
	[table reloadData];
}


#pragma mark ---------------Datasource Methods---------------

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 5;
}


- (float)preferencesTable:(UIPreferencesTable *)table 
			 heightForRow:(int)row 
				  inGroup:(int)group 
	   withProposedHeight:(float)proposed 
{
	if (group == 0 || group == 2) {
        return 10;
    }
	
	
    return proposed;
}


- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	switch (group)
  {
	  case 0: return 1;
	  case 1: return 1;
	  case 2:	return 1;
	  case 3: return 3;
	  case 4: return 1;
		  
  }
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	
	if (group == 0 || group == 2)
	  {
		  
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:@""];
		  [ cell setDrawsBackground: NO ];
		  return [cell autorelease];
	  }				
	else if (group == 1)
	  {
		  
		  
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:[td getCounty]];
		  UIImageView *flagview = [[[UIImageView alloc] initWithFrame: CGRectMake(110.0f, .75f,30.0f, 40.0f)] autorelease];
		  UIImage *flag = nil;
		  if ([[td getCountry] isEqualToString:@"Germany"]) flag = [UIImage applicationImageNamed:@"germanpl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Austria"]) flag = [UIImage applicationImageNamed:@"austriapl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Poland"]) flag = [UIImage applicationImageNamed:@"polandpl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Czech Republic"]) flag = [UIImage applicationImageNamed:@"czpl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Slovakia"]) flag = [UIImage applicationImageNamed:@"skpl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Switzerland"]) flag = [UIImage applicationImageNamed:@"swisspl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Norway"]) flag = [UIImage applicationImageNamed:@"norwegianpl.png"] ;
		  if ([[td getCountry] isEqualToString:@"Italy"]) flag = [UIImage applicationImageNamed:@"italianpl.png"] ;
		  
		  if (flag) {
			  
			  [flagview setImage:flag];
			  [cell addSubview:flagview];
			  
		  }
		  //[flagview release];
		  //[flag release];
		  [cell setAlignment:2];
		  
		  return [cell autorelease];
		  
		  
		  
	  } 
	else if (group == 3) 
	  {
		  switch (row)
		{
			case 0:
		  {
			  UIPreferencesTableCell *citycell = [[UIPreferencesTableCell alloc] init];
			  [citycell setTitle:citycellTitle];
			  [citycell setValue:[td getDescription]];
			  
			  [citycell setAlignment:citycellAlign];
			  if ([[td getDescription] length]>18 && citycellAlign != 2) [citycell setShowDisclosure:YES];
			  
			  return [citycell autorelease];
		  }
			case 1:
		  {
			  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
			  [cell setTitle:@"State"];
			  [cell setValue:[td getState]];
			  [cell setUsesBlueDisclosureCircle:YES];
			  
			  return [cell autorelease];
		  }
			case 2:
		  {
			  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
			  [cell setTitle:@"Country"];
			  [cell setValue:[td getCountry]];
			  [cell setUsesBlueDisclosureCircle:YES];
			  
			  return [cell autorelease];
		  }
		}
	  } 
	else if (group == 4) 
	  {
		  UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		  [cell setTitle:@"Show in Maps"];
		  [cell setShowDisclosure:YES];
		  [cell setAlignment:2];
		  return [cell autorelease];
	  }
	
}



@end